package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TrainingStudyRecord;
import com.ruoyi.system.domain.TrainingStudyRecordCount;
import com.ruoyi.system.service.ITrainingStudyRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 培训学习记录Controller
 *
 * @author dp
 * @date 2024-01-11
 */
@Api(tags = {"培训学习记录管理"})
@RestController
@RequestMapping("/system/record")
public class TrainingStudyRecordController extends BaseController {
    @Autowired
    private ITrainingStudyRecordService trainingStudyRecordService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询培训学习记录列表
     */
    @ApiOperation(value = "培训学习记录列表", notes = "查询培训学习记录列表详情", tags = {"培训学习记录管理"})
    //@PreAuthorize("@ss.hasPermi('system:RECORD:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingStudyRecord trainingStudyRecord) {
        startPage();
        List<TrainingStudyRecord> list = trainingStudyRecordService.selectTrainingStudyRecordList(trainingStudyRecord);
        return getDataTable(list);
    }

    /**
     * 查询培训学习记录合计列表
     */
    @ApiOperation(value = "查询培训学习记录合计列表", notes = "查询培训学习记录列表详情", tags = {"培训学习记录管理"})
    @GetMapping("/countList")
    public TableDataInfo countList(@RequestParam(value = "submitBegin", required = false) String submitBegin,
                                   @RequestParam(value = "submitEnd", required = false) String submitEnd,
                                   @RequestParam("baseId") String baseId,
                                   @RequestParam("notesStatus") String notesStatus,
                                   @RequestParam(value = "cadetId", required = false) Long cadetId,
                                   @RequestParam(value = "cadetName", required = false) String cadetName) {
        startPage();
        List<TrainingStudyRecordCount> list = trainingStudyRecordService.selectTrainingStudyRecordCount(
                submitBegin, submitEnd, baseId, notesStatus, cadetId, cadetName);
        return getDataTable(list);
    }

    @ApiOperation(value = "培训学习记录列表", notes = "查询培训学习记录列表详情", tags = {"培训学习记录管理"})
    @GetMapping("/getRecordList")
    public TableDataInfo getRecordList(@RequestParam(value = "submitBegin",required = false) String submitBegin,
                                       @RequestParam(value = "submitEnd",required = false) String submitEnd,
                                       @RequestParam("baseId") String baseId,
                                       @RequestParam("cadetId") String cadetId,
                                       @RequestParam("notesStatus") String notesStatus) {
        startPage();
        List<TrainingStudyRecord> list = trainingStudyRecordService.getRecordList(submitBegin, submitEnd, baseId, cadetId, notesStatus);
        return getDataTable(list);
    }

    /**
     * 导出培训学习记录列表
     */
    @ApiOperation(value = "培训学习记录列表", notes = "导出培训学习记录列表详情", tags = {"培训学习记录管理"})
    //@PreAuthorize("@ss.hasPermi('system:RECORD:export')")
    @Log(title = "培训学习记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingStudyRecord trainingStudyRecord) {
        List<TrainingStudyRecord> list = trainingStudyRecordService.selectTrainingStudyRecordList(trainingStudyRecord);
        ExcelUtil<TrainingStudyRecord> util = new ExcelUtil<TrainingStudyRecord>(TrainingStudyRecord.class);
        util.exportExcel(response, list, "培训学习记录数据");
    }

    /**
     * 获取培训学习记录详细信息
     */
    @ApiOperation(value = "培训学习记录详细信息", notes = "获取培训学习记录详细信息详情", tags = {"培训学习记录管理"})
    //@PreAuthorize("@ss.hasPermi('system:RECORD:query')")
    @GetMapping(value = "/{studyRecordId}")
    public AjaxResult getInfo(@PathVariable("studyRecordId") Long studyRecordId) {
        return success(trainingStudyRecordService.selectTrainingStudyRecordByStudyRecordId(studyRecordId));
    }

    /**
     * 新增培训学习记录
     */
    @ApiOperation(value = "新增培训学习记录", notes = "新增培训学习记录详情", tags = {"培训学习记录管理"})
    //@PreAuthorize("@ss.hasPermi('system:RECORD:add')")
    @Log(title = "培训学习记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody TrainingStudyRecord trainingStudyRecord) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingStudyRecordService.completeData(trainingStudyRecord, userId, 1);
        return toAjax(trainingStudyRecordService.insertTrainingStudyRecord(trainingStudyRecord));
    }

    /**
     * 修改培训学习记录
     */
    @ApiOperation(value = "修改培训学习记录", notes = "修改培训学习记录详情", tags = {"培训学习记录管理"})
    //@PreAuthorize("@ss.hasPermi('system:RECORD:edit')")
    @Log(title = "培训学习记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody TrainingStudyRecord trainingStudyRecord) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingStudyRecordService.completeData(trainingStudyRecord, userId, 2);
        return toAjax(trainingStudyRecordService.updateTrainingStudyRecord(trainingStudyRecord));
    }

    /**
     * 删除培训学习记录
     */
    @ApiOperation(value = "删除培训学习记录", notes = "删除培训学习记录详情", tags = {"培训学习记录管理"})
    //@PreAuthorize("@ss.hasPermi('system:RECORD:remove')")
    @Log(title = "培训学习记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studyRecordIds}")
    public AjaxResult remove(@PathVariable Long[] studyRecordIds) {
        return toAjax(trainingStudyRecordService.deleteTrainingStudyRecordByStudyRecordIds(studyRecordIds));
    }
}
